package com.sr.controller;

import com.sr.constant.Constant;
import com.sr.ibase.IBaseService;
import com.sr.pojo.WMember;
import com.sr.service.IMemberService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.MemberReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaochi
 * @date 2022/4/27 18:39
 * @desc MemberController
 */
@Api(tags = "后台接口 -> 会员模块")
@RestController
@RequestMapping(Constant.Api.BACK_PREFIX +"/member")
@RequiredArgsConstructor(onConstructor = @__)
public class MemberController extends BaseController{

    private final IMemberService memberService;

    /**
     * 条件查询会员
     * @return
     */
    @ApiOperation(value = "会员列表",notes = "会员列表")
    @PostMapping("/list/page")
    public R<PageVo<WMember>> pageList(@RequestBody MemberReqVo reqVo){
        IBaseService.Criteria<WMember> criteria = memberService.createCriteria(WMember.class);
        criteria.andEqualTo(WMember::getUsername,!StringUtils.isEmpty(reqVo.getUsername()),reqVo.getUsername())
                .andEqualTo(WMember::getNickname,!StringUtils.isEmpty(reqVo.getNickname()),reqVo.getNickname())
                .andEqualTo(WMember::getIsDel,1);
        return R.ok(memberService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    /**
     * 删除会员
     * @return
     */
    @ApiOperation(value = "删除会员",notes = "删除会员")
    @GetMapping("/delete/{id}")
    public R<String> delete(@PathVariable String id){
        return this.response(memberService.updateById(WMember.builder().id(id).isDel(0).build()));
    }
}